package avl.sv.client.tools;

import avl.sv.client.MouseActionLogger;
import avl.sv.client.SlideViewer;
import avl.sv.shared.Annotations.ROI;
import avl.sv.shared.Annotations.ROIRectangle;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import javax.swing.AbstractAction;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;

/**
 *
 * @author benbryan
 */
public class RectangleTool extends AbstractSlideViewerTool {
    private RectangleToolPrompt rectanglePrompt;
    private ROI workingROI = null;
    private final SlideViewer slideViewer;
    private MouseActionLogger mouseAction = new MouseActionLogger();
    
    public RectangleTool(SlideViewer slideViewer) {
        this.slideViewer = slideViewer;
        setToolTipText("Rectangle Tool");
        setIcon(new javax.swing.ImageIcon(getClass().getResource("/avl/sv/client/icon/rectangle.png")));
        Window parentWindow = SwingUtilities.windowForComponent(this);
        rectanglePrompt = new RectangleToolPrompt(parentWindow, Dialog.ModalityType.APPLICATION_MODAL);
        final JPopupMenu menu = new JPopupMenu();
        final JMenuItem jMenuItemSetup = new JMenuItem("Setup");
        final RectangleTool thisButton = this; 
        jMenuItemSetup.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                for (ActionListener listener:thisButton.getActionListeners()){
                    listener.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "popupmenu shown"));
                }
                rectanglePrompt.setVisible(true);
            }
        });
        menu.add(jMenuItemSetup);
        jMenuItemSetup.setVisible(true);
        setComponentPopupMenu(menu);
        
    }
    
    @Override
    public void mouseMoved(MouseEvent e) {
    }
    
    @Override
    public void mouseClicked(MouseEvent e) {
        if (!e.getSource().equals(slideViewer)) {
            return;
        }
        if (e.getButton() != MouseEvent.BUTTON1) {
            return;
        }
        mouseAction.mouseClicked(e);
    }
    
    @Override
    public void mouseReleased(MouseEvent e) {
        if (!e.getSource().equals(slideViewer)) {
            return;
        }
        mouseAction.mouseReleased(e);
        if (mouseAction.getMouseButton1() == MouseActionLogger.ButtonStatus.RELEASED) {
            slideViewer.getRoiManager().addROI(workingROI);
            workingROI = null;
            slideViewer.getToolPanel().setDefaultTool();
        }
        repaint();
    }
    
    @Override
    public void mouseDragged(MouseEvent e) {
        if (!e.getSource().equals(slideViewer)) {
            return;
        }
        mouseAction.mouseDragged(e);
        if (mouseAction.getMouseButton1() == MouseActionLogger.ButtonStatus.PRESSED) {
            Point pImg = slideViewer.displaySpaceToImageSpace(e.getPoint());
            addingNewRoi(pImg);
        }
        slideViewer.repaint();
    }
    
    @Override
    public void mousePressed(MouseEvent e) {
        if (!e.getSource().equals(slideViewer)) {
            return;
        }
        mouseAction.mousePressed(e);
        if (mouseAction.getMouseButton1() == MouseActionLogger.ButtonStatus.PRESSED) {
            Point pImg = slideViewer.displaySpaceToImageSpace(e.getPoint());
            addingNewRoi(pImg);
        }
        slideViewer.repaint();
    }
    
    private void addingNewRoi(Point pImg) {
        
        slideViewer.getRoiManager().setRoisModified(true);
        int ix = (int) pImg.getX();
        int iy = (int) pImg.getY();
        
        if (workingROI == null) {
            int constraints[] = rectanglePrompt.getConstraints();
            if (!(constraints == null)) {
                workingROI = new ROIRectangle(ix, iy, constraints[0], constraints[1]);
                //workingROI = null;
            } else {
                if (workingROI == null) {
                    workingROI = new ROIRectangle(ix, iy);
                }
            }
        } else {
            ((ROIRectangle) workingROI).addSecondPoint(ix, iy);
        }
        
    }
    
    @Override
    public void mouseEntered(MouseEvent e) {
    }
    
    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void paintOnSlideViewer(Graphics2D g) {
        if (workingROI != null){
            slideViewer.concatenateImageToDisplayTransform(g);
            workingROI.paintROI(g, Color.GREEN);
        }
    }
    

}
